<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* clientHandler Class
*
* This class holds everything serializes new data to the client
* like other handlers, it is not instanced
* 
*/

class clientHandler {
	
	public static function change($user,$prop_name,$property) {
		$user->$prop_name = $property; // Make the change
		
		if (is_string($property)) { $user->send($property,$prop_name); }
		else { clientHandler::$prop_name($user); } // Write it to the client
	}
	
	public static function load($user) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($user); // Eventually we will serialize more specifically...
		$tmp += array("in_room"=>clientHandler::serialize_in_room($user->in_room));
		$tmp += array("loaded"=>true); // To show we loaded
		$user->send_data($tmp);
	}
	
	public static function in_room($user) {
		$user->send(clientHandler::serialize_in_room($user->in_room),"in_room");
	}
	
	/* ----------------- Serializers */
	
	// objects consist of simple stuff, so it's easy to serialize them directly
	public static function serializeobject($object) {
		$tmp = array();
		if (isset($object->name)) 			{ $tmp["name"] = $object->name; 				}
		if (isset($object->short_descr)) 	{ $tmp["short_descr"] = $object->short_descr;	}
		if (isset($object->description)) 	{ $tmp["description"] = $object->description;	}
		if (isset($object->image)) 			{ $tmp["image"] = $object->image;				}
		return $tmp;
	}
	
	public static function serialize_visible_person($user) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($user);
		return $tmp;
	}
	
	public static function serialize_visible_exit($exit) {
		$tmp = array();
		$tmp += clientHandler::serializeobject($exit);
		$tmp += array("command"=>$exit->command);
		return $tmp;
	}
	
	public static function serialize_in_room($room) {
		$tmp = array();
		$tmp2 = array();
		$tmp += clientHandler::serializeobject($room); // Easy.
		
		// The people in the room
		if (isset($room->people)) {
			$i = 0;
			// This will eventually have to call out to see how much information
			// the user is allowed to know about the given person
			foreach($room->people as $person) {
				$tmp2 += array($i=>clientHandler::serialize_visible_person($person));
				$i++;
			}
			$tmp += array("people"=>$tmp2);
		}
		
		$tmp2 = array();
		// The Exits in the room
		if (isset($room->exits)) {
			$i = 0;
			// This will eventually have to call out to see how much information
			// the user is allowed to know about the given person
			foreach($room->exits as $exit) {
				$tmp2 += array($i=>clientHandler::serialize_visible_exit($exit));
				$i++;
			}
			$tmp += array("exits"=>$tmp2);
		}
		
		return $tmp;
	}
}

?>